import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/view/examination/question/fill_blank_question_widget.dart';
import 'package:kxy_flutter_app/view/examination/question/multi_choice_question_widget.dart';
import 'package:kxy_flutter_app/view/examination/question/single_choice_question_widget.dart';
import 'package:kxy_flutter_app/view/examination/question/true_or_false_question_widget.dart';

import '../../../model/exam_question.dart';

class QuestionWidget extends StatelessWidget {
  final Question question;
  final Function(String) checkCallback;
  final bool withResult;

  const QuestionWidget({
    Key key,
    this.question,
    this.checkCallback,
    this.withResult = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (question.type == 1) {
      return SingleChoiceQuestionWidget(
        question: question,
        checkCallback: checkCallback,
        withResult: withResult,
      );
    }
    if (question.type == 2) {
      return MultiChoiceQuestionWidget(
        question: question,
        checkCallback: checkCallback,
        withResult: withResult,
      );
    }
    if (question.type == 3) {
      return TrueOrFalseQuestionWidget(
        question: question,
        checkCallback: checkCallback,
        withResult: withResult,
      );
    }
    if (question.type == 4) {
      return FillBlankQuestionWidget(
        question: question,
        onChange: (strList) {
          var str = jsonEncode(strList);
          checkCallback(str);
        },
        withResult: withResult,
      );
    }

    return Center(
      child: Text('此题已被删除'),
    );
  }
}
